package com.util.library.network;

import com.hethongviet.library.R;
import com.util.library.common.ConfigLib;
import com.util.library.model.Transaction;
import com.util.library.msg.DialogMessageBack;
import com.util.library.network.ConstantNetwork.STATUS;

import android.app.Activity;
import android.app.Dialog;

public abstract class OnNetworkHandleListener extends Activity {
	public abstract void onConnectFail(int typeError);

	public abstract void onNetworkMessageIncomming(Transaction transaction);

	public Dialog createDialogStatusServer(int status) {
		if (ConfigLib.IS_DEBUG) {
			if (status == R.string.message_server_send_ok) {
				return new DialogMessageBack(this, false,
						R.string.message_server_send_ok, false);
			} else {
				return new DialogMessageBack(this, true, status, false);
			}
		} else {
			if (status == R.string.message_server_send_ok) {
				return new DialogMessageBack(this, false,
						R.string.message_server_send_ok, false);
			} else if (status == R.string.message_error_network_connect) {
				return new DialogMessageBack(this, true,
						R.string.message_error_network_connect, false);
			} else if (status == R.string.message_error_network_host_not_exist) {

				return new DialogMessageBack(this, true,
						R.string.message_error_network_host_not_exist, false);
			} else if (status == R.string.message_error_network_io) {
				return new DialogMessageBack(this, true,
						R.string.message_error_network_io, false);
			} else {
				return new DialogMessageBack(this, true,
						R.string.message_server_send_fail, false);
			}
		}
	}

	public void showDialogWithCatch(final int id) {
		runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					showDialog(id);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	public void showDialogWithStatus(final int status) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (ConfigLib.IS_DEBUG) {
					switch (status) {
					case STATUS.SERVER_DB_CREATE_FAIL:
						showDialogWithCatch(R.string.message_server_fail_create_db);
						break;
					case STATUS.SERVER_DB_INSERT_FAIL:
						showDialogWithCatch(R.string.message_server_fail_insert_db);
						break;
					case STATUS.SERVER_SEND_FAIL_DB:
						showDialogWithCatch(R.string.message_server_send_fail_db);
						break;
					case STATUS.SERVER_SEND_FAIL_DB_LOAD:
						showDialogWithCatch(R.string.message_server_send_fail_db_load);
						break;
					case STATUS.SERVER_SEND_FAIL_JSON:
						showDialogWithCatch(R.string.message_server_send_fail_json_load);
						break;
					case STATUS.SERVER_SEND_FAIL_JSON_VALUE:
						showDialogWithCatch(R.string.message_server_send_fail_json_value);
						break;
					case STATUS.SERVER_SEND_FAIL_VALUE_NULL:
						showDialogWithCatch(R.string.message_server_send_fail_value_null);
						break;
					case STATUS.SERVER_SEND_OK:
						showDialogWithCatch(R.string.message_server_send_ok);
						break;
					case ConstantNetwork.CONNECT:
						showDialogWithCatch(R.string.message_error_network_connect);
						break;
					case ConstantNetwork.HOST:
						showDialogWithCatch(R.string.message_error_network_host_not_exist);
						break;
					case ConstantNetwork.IO:
						showDialogWithCatch(R.string.message_error_network_io);
						break;

					// case STATUS.SERVER_SEND_UNKNOWN_TYPE:
					default:
						showDialogWithCatch(R.string.message_server_send_fail_unknown_type);

					}
				} else {
					switch (status) {
					case STATUS.SERVER_DB_CREATE_FAIL:
					case STATUS.SERVER_DB_INSERT_FAIL:
					case STATUS.SERVER_SEND_FAIL_DB:
					case STATUS.SERVER_SEND_FAIL_DB_LOAD:
					case STATUS.SERVER_SEND_FAIL_JSON:
					case STATUS.SERVER_SEND_FAIL_JSON_VALUE:
					case STATUS.SERVER_SEND_FAIL_VALUE_NULL:
					case STATUS.SERVER_SEND_UNKNOWN_TYPE:
						showDialogWithCatch(R.string.message_server_send_fail);
						break;
					case STATUS.SERVER_SEND_OK:
						showDialogWithCatch(R.string.message_server_send_ok);
						break;
					case ConstantNetwork.CONNECT:
						showDialogWithCatch(R.string.message_error_network_connect);
						break;
					case ConstantNetwork.HOST:
						showDialogWithCatch(R.string.message_error_network_host_not_exist);
						break;
					case ConstantNetwork.IO:
						showDialogWithCatch(R.string.message_error_network_io);
						break;
					default:
						showDialogWithCatch(R.string.message_server_send_fail_unknown_type);
					}
				}
			}
		});

	}

}
